package org.yray.io.sentinel;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class SentinelTest {
    public static void main(String[] args)  {
        AtomicInteger i=new AtomicInteger(0);
        AtomicInteger j=new AtomicInteger(0);
        initFlowRules();
        while (true) {
            SentinelThread sentinelThread=new SentinelThread(i,j);
            sentinelThread.start();
            try {
                Thread.sleep(40);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private static void initFlowRules(){
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("HelloWorld");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // Set limit QPS to 20.
        rule.setCount(30);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}
